#include "Mesh_PCH.h"
#include "ISurface.h"

/*--------------------------------------------------------------------------*/

ISurface::ISurface(IModel* model, uint id, uint resources, uint numVertices, uint numIndices) :
	m_position0(NULL),
	m_position1(NULL),
	m_normal0(NULL),
	m_normal1(NULL),
	m_texcoord0(NULL),
	m_texcoord1(NULL),
	m_instancing(NULL),
	m_color0(NULL),
	m_color1(NULL),
	m_resources(resources),
	m_vertexdata(NULL),
	m_numIndices(numIndices),
	m_numVertices(numVertices),
	m_vertexSize(0),
	m_model(model),
	m_id(id)
{
	if (m_resources & EVertex::Position0)
	{
		m_vertexSize += sizeof(glm::vec3);
	}

	if (m_resources & EVertex::Position1)
	{
		m_vertexSize += sizeof(glm::vec3);
	}

	/*--------------------------------------------------------------------------*/

	if (resources & EVertex::Normal0)
	{
		m_vertexSize += sizeof(glm::vec3);
	}

	if (resources & EVertex::Normal1)
	{
		m_vertexSize += sizeof(glm::vec3);
	}

	/*--------------------------------------------------------------------------*/

	if (resources & EVertex::TexCoord0)
	{
		m_vertexSize += sizeof(glm::vec2);
	}

	if (resources & EVertex::TexCoord1)
	{
		m_vertexSize += sizeof(glm::vec2);
	}

	/*--------------------------------------------------------------------------*/

	if (resources & EVertex::Color0)
	{
		m_vertexSize += sizeof(glm::vec4);
	}

	if (resources & EVertex::Color1)
	{
		m_vertexSize += sizeof(glm::vec4);
	}

	/*--------------------------------------------------------------------------*/

	if (resources & EVertex::Instancing)
	{
		m_vertexSize += sizeof(glm::mat4);
	}

	/*--------------------------------------------------------------------------*/
}

/*--------------------------------------------------------------------------*/

ISurface::~ISurface()
{
	if (m_indices)
	{
		delete [] m_indices;
		m_indices = NULL;
	}

	if (m_id != -1 && m_vertexdata)
	{
		delete [] m_vertexdata;
		m_vertexdata = NULL;
		m_position0	= NULL;
		m_position1 = NULL;
		m_normal0 = NULL;
		m_normal1 = NULL;
		m_texcoord0 = NULL;
		m_texcoord1 = NULL;
		m_color0 = NULL;
		m_color1 = NULL;
		m_instancing = NULL;
	}
}

/*--------------------------------------------------------------------------*/

ushort* ISurface::GetIndices()
{
	return m_indices;
}

/*--------------------------------------------------------------------------*/

glm::vec3* ISurface::vPosition0()
{
	return m_position0;
}

/*--------------------------------------------------------------------------*/

glm::vec3* ISurface::vPosition1()
{
	return m_position1;
}

/*--------------------------------------------------------------------------*/

glm::vec3* ISurface::vNormal0()
{
	return m_normal0;
}

/*--------------------------------------------------------------------------*/

glm::vec3* ISurface::vNormal1()
{
	return m_normal1;
}

/*--------------------------------------------------------------------------*/

glm::vec2* ISurface::vTexcoord0()
{
	return m_texcoord0;	
}

/*--------------------------------------------------------------------------*/

glm::vec2* ISurface::vTexcoord1()
{
	return m_texcoord1;
}

/*--------------------------------------------------------------------------*/

glm::vec4* ISurface::vColor0()
{
	return m_color0;
}

/*--------------------------------------------------------------------------*/

glm::vec4* ISurface::vColor1()
{
	return m_color1;
}

/*--------------------------------------------------------------------------*/

glm::mat4* ISurface::vInstancing()
{
	return m_instancing;
}

/*--------------------------------------------------------------------------*/

uint ISurface::GetResources()
{
	return m_resources;
}

/*--------------------------------------------------------------------------*/

uint ISurface::GetNumVertives()
{
	return m_numVertices;
}

/*--------------------------------------------------------------------------*/

uint ISurface::GetNumIndices()
{
	return m_numIndices;
}

/*--------------------------------------------------------------------------*/

uint ISurface::GetSize()
{
	return m_vertexSize * m_numVertices;
}

/*--------------------------------------------------------------------------*/

uchar* ISurface::GetVertexData()
{
	return m_vertexdata;
}

/*--------------------------------------------------------------------------*/

void ISurface::SetMemory( uchar* ptn )
{	
	__Check((m_vertexdata == NULL && ptn != NULL) || (m_vertexdata == ptn && ptn != NULL));
	m_vertexdata = ptn;

	if (m_resources & EVertex::Indices && m_numIndices)
	{
		m_indices = new ushort[m_numIndices];
	}

	/*--------------------------------------------------------------------------*/

	if (m_resources & EVertex::Position0)
	{
		m_position0 = (glm::vec3*) ptn;
		ptn += sizeof(glm::vec3) * m_numVertices;
	}

	if (m_resources & EVertex::Position1)
	{
		m_position1 = (glm::vec3*) ptn;
		ptn += sizeof(glm::vec3) * m_numVertices;
	}

	/*--------------------------------------------------------------------------*/

	if (m_resources & EVertex::Normal0)
	{
		m_normal0 = (glm::vec3*) ptn;
		ptn += sizeof(glm::vec3) * m_numVertices;
	}

	if (m_resources & EVertex::Normal1)
	{
		m_normal1 = (glm::vec3*) ptn;
		ptn += sizeof(glm::vec3) * m_numVertices;
	}

	/*--------------------------------------------------------------------------*/

	if (m_resources & EVertex::TexCoord0)
	{
		m_texcoord0 = (glm::vec2*) ptn;
		ptn += sizeof(glm::vec2) * m_numVertices;
	}

	if (m_resources & EVertex::TexCoord1)
	{
		m_texcoord1 = (glm::vec2*) ptn;
		ptn += sizeof(glm::vec2) * m_numVertices;
	}

	/*--------------------------------------------------------------------------*/

	if (m_resources & EVertex::Color0)
	{
		m_color0 = (glm::vec4*) ptn;
		ptn += sizeof(glm::vec4) * m_numVertices;
	}

	if (m_resources & EVertex::Color1)
	{
		m_color1 = (glm::vec4*) ptn;
		ptn += sizeof(glm::vec4) * m_numVertices;
	}

	/*--------------------------------------------------------------------------*/

	if (m_resources & EVertex::Instancing)
	{
		m_instancing = (glm::mat4*) ptn;
		ptn += sizeof(glm::mat4) * m_numVertices;
	}

	/*--------------------------------------------------------------------------*/
}

/*--------------------------------------------------------------------------*/

IModel* ISurface::GetModel()
{
	return m_model;
}

/*--------------------------------------------------------------------------*/

uint ISurface::GetId()
{
	return m_id;
}

/*--------------------------------------------------------------------------*/

const std::string& ISurface::GetName() const
{
	return m_name;
}

/*--------------------------------------------------------------------------*/
